<?php

/**
 * 服务器监听用户动作
 */

$serv->on('Message', function($server, $frame) {

    //推送用户在线列表
    if ($frame->data == 'getonlines') {
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
        }

    //新进用户加入列表
    } else if (substr($frame->data, 0, 8) == 'mystatus') {

        $info = json_decode(substr($frame->data, 8));
        $GLOBALS['userlist'][$frame->fd] = $info;

    //推送大厅座位情况
    } else if (substr($frame->data, 0, 10) == 'tableinfos') {
        
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
        }

    //推送是否允许坐下
    } else if (substr($frame->data, 0, 8) == 'seatdown') {
        
        $info = json_decode(substr($frame->data, 8));
        if ($GLOBALS['hall'][$info->table][$info->seat] == 'off') {
            $server->push($frame->fd, 'seatdown'.'ok');
        } else {
            $server->push($frame->fd, 'seatdown'.'no');
        }

    //将加入房间的玩家信息更新到数组
    } else if (substr($frame->data, 0, 11) == 'getroominfo') {
        
        //写入用户状态信息
        $info = json_decode(substr($frame->data, 11));
        $GLOBALS['hall'][$info->roomid][$info->seatid] = $frame->fd;
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_nick'] = $info->user_nick;
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = $info->mystatus;
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_id'] = $info->uid;
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_point'] = getuserwinlose($info->uid);
        //如果房间数组不存在，初始化
        if (!isset($GLOBALS['rooms'][$info->roomid])) {
            $GLOBALS['rooms'][$info->roomid] = [];
        }
        
        //判断是重新开局还是新进玩家
        $isreopen = false;
        foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
            if ($v['fd'] == $frame->fd) {
                $isreopen = true;
                break;
            }            
        }

        //判断加入房间的是第几个 并加入游戏顺序
        if ($isreopen == false) {
            if (count($GLOBALS['rooms'][$info->roomid]) == 0) {
                $GLOBALS['rooms'][$info->roomid][0] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
            } else if (count($GLOBALS['rooms'][$info->roomid]) == 1) {
                $GLOBALS['rooms'][$info->roomid][1] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
            } else if (count($GLOBALS['rooms'][$info->roomid]) == 2) {
                $GLOBALS['rooms'][$info->roomid][2] = ['fd'=>$frame->fd,'nick'=>$info->user_nick];
            }
        }

        //广播到该房间所有的用户 广播到全局
        $fd1 = $GLOBALS['hall'][$info->roomid]['user_a'];
        $fd2 = $GLOBALS['hall'][$info->roomid]['user_b'];
        $fd3 = $GLOBALS['hall'][$info->roomid]['user_c'];
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
            if ($v == $fd3 || $v == $fd1 || $v == $fd2) {
                $server->push($v, 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
            }
        }
        $server->push($frame->fd, 'roomchat'.'{"nick":"系统消息","msg":"欢迎来到世界上最Low的在线斗地主!"}');
    //改变准备状态
    } else if (substr($frame->data, 0, 15) == 'pushreadystatus') {

        // 等待 || 准备
        if (substr($frame->data, 15, 2) == 'ok') {
            $info = json_decode(substr($frame->data, 17));
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = '准备';
            $GLOBALS['userlist'][$frame->fd]->status = '准备';
        } else if (substr($frame->data, 15, 2) == 'no') {
            $info = json_decode(substr($frame->data, 17));
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_status'] = '等待';
            $GLOBALS['userlist'][$frame->fd]->status = '等待';
            $GLOBALS['hall'][$info->roomid]['status'] = 'off';
        }

        //刷新房间内信息变动
        if (isset($info)) {
            $fd1 = $GLOBALS['hall'][$info->roomid]['user_a'];
            $fd2 = $GLOBALS['hall'][$info->roomid]['user_b'];
            $fd3 = $GLOBALS['hall'][$info->roomid]['user_c'];
            //如果三个人状态都在准备中 推送开始游戏消息
            if ($GLOBALS['hall'][$info->roomid]['user_a_status'] == '准备' && 
                $GLOBALS['hall'][$info->roomid]['user_b_status'] == '准备' &&
                $GLOBALS['hall'][$info->roomid]['user_c_status'] == '准备') {
                //初始化房间数据
                resetRoom($info->roomid);
                //修改游戏状态 并且推送
                $GLOBALS['hall'][$info->roomid]['user_a_status'] = '游戏中';
                $GLOBALS['hall'][$info->roomid]['user_b_status'] = '游戏中';
                $GLOBALS['hall'][$info->roomid]['user_c_status'] = '游戏中';
                //更新游戏胜率
                $GLOBALS['userlist'][$fd1]->point = getuserwinlose($GLOBALS['userlist'][$fd1]->uid);
                $GLOBALS['userlist'][$fd2]->point = getuserwinlose($GLOBALS['userlist'][$fd2]->uid);
                $GLOBALS['userlist'][$fd3]->point = getuserwinlose($GLOBALS['userlist'][$fd3]->uid);
                //更新游戏状态
                $GLOBALS['userlist'][$fd1]->status = '游戏中';
                $GLOBALS['userlist'][$fd2]->status = '游戏中';
                $GLOBALS['userlist'][$fd3]->status = '游戏中';
                $GLOBALS['hall'][$info->roomid]['status'] = 'on';
                $server->push($fd1, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
                $server->push($fd2, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
                $server->push($fd3, 'gostartgame'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
            }
            //大厅座位信息广播到全局 用户列表刷新
            foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
                $server->push($v['fd'], 'freshorder'.json_encode([$GLOBALS['rooms'][$info->roomid],$GLOBALS['hall'][$info->roomid]]));
            }
        }

        //广播房间准备状态到全局
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
            $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
        }

    //广播房间内的消息
    } else if (substr($frame->data, 0, 8) == 'roomchat') {
        $info = json_decode(substr($frame->data, 8));
        foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
            $server->push($v['fd'], 'roomchat'.substr($frame->data, 8));
        }

    //广播全局聊天
    } else if (substr($frame->data, 0, 9) == 'publicmsg') {
        foreach ($GLOBALS['fds'] as $v) {
            $server->push($v, $frame->data);
        }

    //接受确认游戏的信息并推送对局初始化    
    } else if (substr($frame->data, 0,8) == 'letsplay') {

        $info = json_decode(substr($frame->data, 8));

        if (count($GLOBALS['hall'][$info->roomid]['pokers']) == 0) {
            $GLOBALS['hall'][$info->roomid]['pokers'] = $GLOBALS['poker_list'];
        }

        //发牌
        $pokers = array_rand($GLOBALS['hall'][$info->roomid]['pokers'], 17);
        foreach ($pokers as $k => $v) {
            foreach ($GLOBALS['hall'][$info->roomid]['pokers'] as $a => $s) {
                if ($v == $a) {
                    unset($GLOBALS['hall'][$info->roomid]['pokers'][$a]);
                }
            }
        }

        $GLOBALS['hall'][$info->roomid][$info->seatid.'_pokers'] = $pokers;
        $temp = [];
        foreach ($pokers as $a => $s) {
            $temp[] = ['poker'=>$s, 'value'=>$GLOBALS['poker_list'][$s]];
        }

        $server->push($GLOBALS['hall'][$info->roomid][$info->seatid], 'fapaile'.json_encode($temp));

        //第一顺位玩家初始化顺位
        if ($GLOBALS['rooms'][$info->roomid][0]['fd'] == $frame->fd) {
            //初始化顺位
            $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
            $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][0]['nick'].'"}';
            //让顺位0用户开始叫地主
            $server->push($GLOBALS['rooms'][$info->roomid][0]['fd'], 'jiaobujiao');
            //其它两个用户开始等待
            $server->push($GLOBALS['rooms'][$info->roomid][1]['fd'], $waiting);
            $server->push($GLOBALS['rooms'][$info->roomid][2]['fd'], $waiting);
        }

    //用户放弃叫地主事件
    } else if (substr($frame->data, 0,8) == 'wobujiao') {
        $info = json_decode(substr($frame->data, 8));
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '不叫';

        //如果三个人都不叫 则重新发牌
        if ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫' &&
            $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫' &&
            $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') {
            //重置房间
            resetRoom($info->roomid);
            //循环广播重开游戏事件
            foreach ($GLOBALS['rooms'][$info->roomid] as $v) {
                $server->push($v['fd'], 'nobodystandup');
            }
        
        //否则开始下一位叫牌    
        } else {
            //刷新出牌次序
            if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
                $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
            } else {
                $GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
            }
            //拿到下一位
            $turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
            $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';

            //推送叫牌给下一位
            foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                if ($turn == $k) {
                    $server->push($v['fd'], 'jiaobujiao');
                }
                $server->push($v['fd'], 'tabujiao'.$info->nick);
                $server->push($v['fd'], $waiting);
            }
        }

    //有人叫地主
    } else if (substr($frame->data, 0,6) == 'wojiao') {
        $info = json_decode(substr($frame->data, 6));
        $GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '叫地主';

        //如果已经有两个人不叫了 他叫他就是地主
        if ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫' && 
            $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') {
            $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '地主';
            $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
            $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';

            //通知地主他是地主 通知对手等待他出牌
            foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                $server->push($v['fd'], 'dizhuis'.$info->nick);
                $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                if ($v['fd'] != $frame->fd) {
                    $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_c_nick'].'"}');
                } else {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                }
            }
            

        } else if ($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫' && 
                   $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') {
            $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '地主';
            $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
            $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';

            //通知地主他是地主 通知对手等待他出牌
            foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                $server->push($v['fd'], 'dizhuis'.$info->nick);
                $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                if ($v['fd'] != $frame->fd) {
                    $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_a_nick'].'"}');
                } else {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                }
            }
            

        } else if ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫' && 
                   $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫'){
            $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '地主';
            $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
            $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';

            //通知地主他是地主 通知对手等待他出牌
            foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                $server->push($v['fd'], 'dizhuis'.$info->nick);
                $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                if ($v['fd'] != $frame->fd) {
                    $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_b_nick'].'"}');
                } else {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                }
            }
            

        //开始抢地主
        } else {

            //刷新叫次序
            if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
                $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
            } else {
                $GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
            }

            //拿到下一位
            $turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
            $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';

            //推送抢地主给下一位
            foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                if ($turn == $k) {
                    $server->push($v['fd'], 'qiangbuqiang');
                }
                //告诉所有人他叫了,等待下一位
                $server->push($v['fd'], 'tajiaole'.$info->nick);
                $server->push($v['fd'], $waiting);
            }

        }
    
    //不抢地主
    } else if (substr($frame->data, 0,9) == 'wobuqiang') {
        $info = json_decode(substr($frame->data, 9));

        //如果叫地主的人不抢了 第一个抢地主的人就是地主
        if ($GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] == '叫地主') {
            //找出第一个叫地主的人
            foreach ($GLOBALS['hall'][$info->roomid] as $k => $v) {
                if ($v === '抢地主') {
                    //取得地主昵称
                    $nick = $GLOBALS['hall'][$info->roomid][substr($k, 0, 6).'_nick'];
                    //通知地主他是地主 通知对手等待他出牌
                    foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                        $server->push($v['fd'], 'dizhuis'.$nick);
                        $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                        if ($v['nick'] != $nick) {
                            $server->push($v['fd'], 'waiting'.'{"nick":"'.$nick.'"}');
                        } else {
                            $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                        }
                    }
                    break;
                }
            }

        //继续判断
        } else {

            $GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '不抢';

            //如果已经有两个人不抢也不叫地主了 那么剩下一个人就是地主 
            if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' || 
                 $GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
                ($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
                 $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') &&
                ($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '叫地主' ||
                 $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '抢地主')) {
                $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '地主';
                $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
                $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';

                //通知地主他是地主 通知对手等待他出牌
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    $server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_c_nick']);
                    $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                    if ($v['fd'] != $frame->fd) {
                        $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_c_nick'].'"}');
                    } else {
                        $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                    }
                }

            } else if (($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
                        $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') && 
                       ($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' || 
                        $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
                       ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '叫地主' ||
                        $GLOBALS['hall'][$info->roomid]['user_a_identity'] == '抢地主')) {
                $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '地主';
                $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
                $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';

                //通知地主他是地主 通知对手等待他出牌
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    $server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_a_nick']);
                    $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                    if ($v['fd'] != $frame->fd) {
                        $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_a_nick'].'"}');
                    } else {
                        $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                    }
                }

            } else if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' ||
                        $GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
                       ($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' ||
                        $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
                       ($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '叫地主' ||
                        $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '抢地主')){
                $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '地主';
                $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
                $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';

                //通知地主他是地主 通知对手等待他出牌
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    $server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_b_nick']);
                    $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                    if ($v['fd'] != $frame->fd) {
                        $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_b_nick'].'"}');
                    } else {
                        $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                    }
                }

            } else {
                //刷新抢次序
                if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
                } else {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
                }

                $turn = $GLOBALS['hall'][$info->roomid]['whosturn'];

                $waiting = 'waiting'.'{"nick":"'.$GLOBALS['rooms'][$info->roomid][$turn]['nick'].'"}';

                //推送抢地主给下一位
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    if ($turn == $k) {
                        $server->push($v['fd'], 'qiangbuqiang');
                    }
                    //告诉所有人他抢了,等待下一位
                    $server->push($v['fd'], 'tabuqiang'.$info->nick);
                    $server->push($v['fd'], $waiting);
                }
                
            }
        }

    //抢地主
    } else if (substr($frame->data, 0,7) == 'woqiang') {
        $info = json_decode(substr($frame->data, 7));
        $GLOBALS['hall'][$info->roomid]['game_double'] = $GLOBALS['hall'][$info->roomid]['game_double'] * 2;

        //如果叫地主的人在抢地主了 那么地主就是他
        if ($GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] == '叫地主') {
            $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
            $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';
            $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
            $GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '地主';

            //通知地主他是地主 通知对手等待他出牌
            foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                $server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid][$info->seatid.'_nick']);
                $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                if ($v['fd'] != $frame->fd) {
                    $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid][$info->seatid].'"}');
                } else {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                }
            }

        //继续判断
        } else {

            $GLOBALS['hall'][$info->roomid][$info->seatid.'_identity'] = '抢地主';

             //如果已经有两个人不抢也不叫了 谁抢谁就是地主
            if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' || 
                 $GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
                ($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
                 $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') &&
                 ($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '叫地主' ||
                  $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '抢地主')) {
                $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '地主';
                $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
                $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';

                //通知地主他是地主 通知对手等待他出牌
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    $server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_c_nick']);
                    $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                    if ($v['fd'] != $frame->fd) {
                        $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_c_nick'].'"}');
                    } else {
                        $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                    }
                }

            } else if (($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不抢' ||
                        $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '不叫') && 
                       ($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' || 
                        $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
                       ($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '叫地主' ||
                        $GLOBALS['hall'][$info->roomid]['user_a_identity'] == '抢地主')) {
                $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '地主';
                $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';
                $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '农民';

                //通知地主他是地主 通知对手等待他出牌
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    $server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_a_nick']);
                    $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                    if ($v['fd'] != $frame->fd) {
                        $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_a_nick'].'"}');
                    } else {
                        $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                    }
                }

            } else if (($GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不抢' ||
                        $GLOBALS['hall'][$info->roomid]['user_a_identity'] == '不叫') &&
                       ($GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不抢' ||
                        $GLOBALS['hall'][$info->roomid]['user_c_identity'] == '不叫') &&
                       ($GLOBALS['hall'][$info->roomid]['user_b_identity'] == '叫地主' ||
                        $GLOBALS['hall'][$info->roomid]['user_b_identity'] == '抢地主')){
                $GLOBALS['hall'][$info->roomid]['user_b_identity'] = '地主';
                $GLOBALS['hall'][$info->roomid]['user_a_identity'] = '农民';
                $GLOBALS['hall'][$info->roomid]['user_c_identity'] = '农民';

                //通知地主他是地主 通知对手等待他出牌
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    $server->push($v['fd'], 'dizhuis'.$GLOBALS['hall'][$info->roomid]['user_b_nick']);
                    $server->push($v['fd'], 'sanzhang'.json_encode($GLOBALS['hall'][$info->roomid]['pokers']));
                    if ($v['fd'] != $frame->fd) {
                        $server->push($v['fd'], 'waiting'.'{"nick":"'.$GLOBALS['hall'][$info->roomid]['user_b_nick'].'"}');
                    } else {
                        $GLOBALS['hall'][$info->roomid]['whosturn'] = $k;
                    }
                }
                
            } else {

                //刷新抢次序
                if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
                } else {
                    $GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
                }
                 //拿到下一位次序
                $turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
                //拿到下一位昵称
                $next_nick = $GLOBALS['rooms'][$info->roomid][$turn]['nick'];
                //拼接等待字符串
                $waiting = 'waiting'.'{"nick":"'.$next_nick.'"}';

                //推送抢地主给下一位
                foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
                    if ($turn == $k) {
                        $server->push($v['fd'], 'qiangbuqiang');
                    }
                    //告诉所有人他抢了,等待下一位
                    $server->push($v['fd'], 'taqiangle'.$info->nick);
                    $server->push($v['fd'], $waiting);
                }
            }
        }

    //我出牌
    } else if (substr($frame->data, 0,8) == 'wochupai') {

        $info = json_decode(substr($frame->data, 8), true);
        //拿到扑克数组
        $pokers = $info['pokers'][0];
        //取得完整扑克键值对
        $temp = [];
        //取得值
        $numbers = [];

        foreach ($pokers as $k => $v) {
            $temp[] = ['poker'=>$v,'value'=>$GLOBALS['poker_list'][$v]];
            $numbers[] = $GLOBALS['poker_list'][$v];
        }

        //判断是什么牌型
        $res = checkpokers($numbers);
        //如果牌型正确 继续操作
        if ($res != false) {

            $res = json_decode($res);

            $round_type = $GLOBALS['hall'][$info['roomid']]['round_type'];

            if ($GLOBALS['hall'][$info['roomid']]['round_type'] == 'off' || checkdaxiao($round_type, $res) ||
                $GLOBALS['hall'][$info['roomid']]['whospoker'] == $info['nick'] || 
                $res->type == 'huojian') {

                $server->push($frame->fd, 'chupaiok'.json_encode($pokers));

                //标记最后出牌的人是谁
                $GLOBALS['hall'][$info['roomid']]['whospoker'] = $info['nick'];
                //保存这一手的类型
                $GLOBALS['hall'][$info['roomid']]['round_type'] = $res;

                foreach ($pokers as $r) {
                    foreach ($GLOBALS['hall'][$info['roomid']][$info['seatid'].'_pokers'] as $k => $v) {
                        if ($v == $r) {
                            array_splice($GLOBALS['hall'][$info['roomid']][$info['seatid'].'_pokers'], $k, 1);
                        }
                    }
                }

                //如果玩家还有牌
                if (count($GLOBALS['hall'][$info['roomid']][$info['seatid'].'_pokers']) > 0) {
                    //刷新出牌次序
                    if ($GLOBALS['hall'][$info['roomid']]['whosturn'] == count($GLOBALS['rooms'][$info['roomid']]) - 1) {
                        $GLOBALS['hall'][$info['roomid']]['whosturn'] = 0;
                    } else {
                        $GLOBALS['hall'][$info['roomid']]['whosturn'] = $GLOBALS['hall'][$info['roomid']]['whosturn'] + 1;
                    }

                    //拿到下一位次序
                    $turn = $GLOBALS['hall'][$info['roomid']]['whosturn'];
                    //拿到下一位昵称
                    $next_nick = $GLOBALS['rooms'][$info['roomid']][$turn]['nick'];
                    //拼接等待字符串
                    $waiting = 'waiting'.'{"nick":"'.$next_nick.'"}';

                    
                    foreach ($GLOBALS['rooms'][$info['roomid']] as $k => $v) {
                        //广播是谁出的
                        $server->push($v['fd'], 'lasthand'.$info['nick']);
                        //告诉所有人他出了 广播牌队列
                        $server->push($v['fd'], 'chulepai'.json_encode($temp));
                        //等待下一位
                        $server->push($v['fd'], $waiting);
                    }

                    //推送出牌给下一位
                    foreach ($GLOBALS['rooms'][$info['roomid']] as $k => $v) {
                        if ($turn == $k) {
                            $server->push($v['fd'], 'chupaile');
                        }
                    }

                } else {
                    //调用计分结算方法 返回win和lose数组
                    $res = gameOver($info['roomid'], $GLOBALS['hall'][$info['roomid']][$info['seatid'].'_identity']);

                    //广播结算到整个房间 修改玩家状态
                    foreach($GLOBALS['rooms'][$info['roomid']] as $v) {
                        $GLOBALS['userlist'][$v['fd']]->status = '等待';
                        $server->push($v['fd'], 'gameover'.json_encode($res));
                        $server->push($v['fd'],'freshorder'.json_encode([$GLOBALS['rooms'][$info['roomid']],$GLOBALS['hall'][$info['roomid']]]));
                    }

                    //广播房间准备状态到全局
                    foreach ($GLOBALS['fds'] as $v) {
                        $server->push($v, 'pulllist'.json_encode($GLOBALS['userlist']));
                        $server->push($v, 'pulltables'.json_encode($GLOBALS['hall']));
                    }
                }
            }
        }
        

    //我不出牌
    } else if (substr($frame->data, 0, 7) == 'wobuchu') {
        $info = json_decode(substr($frame->data, 7));

        //刷新出次序
        if ($GLOBALS['hall'][$info->roomid]['whosturn'] == count($GLOBALS['rooms'][$info->roomid]) - 1) {
            $GLOBALS['hall'][$info->roomid]['whosturn'] = 0;
        } else {
            $GLOBALS['hall'][$info->roomid]['whosturn'] = $GLOBALS['hall'][$info->roomid]['whosturn'] + 1;
        }

        //拿到下一位次序
        $turn = $GLOBALS['hall'][$info->roomid]['whosturn'];
        //拿到下一位昵称
        $next_nick = $GLOBALS['rooms'][$info->roomid][$turn]['nick'];
        //拼接等待字符串
        $waiting = 'waiting'.'{"nick":"'.$next_nick.'"}';

        //推送不出牌给下一位
        foreach ($GLOBALS['rooms'][$info->roomid] as $k => $v) {
            if ($turn == $k) {
                $server->push($v['fd'], 'chupaile');
            }
            //告诉所有人他不出,等待下一位
            $server->push($v['fd'], 'tabuchu'.$info->nick);
            $server->push($v['fd'], $waiting);
        }
    
    } else if (substr($frame->data, 0, 7) == '') {
        $info = json_decode(substr($frame->data, 7));
    }



});
